package com.itheima.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 监听生产者消息并处理
 *
 * @Author mr.zhang
 * Date 2025/6/21 10:33
 */
@Component
@Slf4j
public class SpringAMQPListener {


    /**
     * 接收简单消息队列,默认接收后立刻处理
     * @param msg
     */
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleMsg(String msg){
        log.info("消费者监听到简单消息:{}",msg);
    }



    /**
     * 接收工作队列消息1
     */
    @RabbitListener(queues = "work.queue")
    public void workQueueListener1(String msg){
        try {
            TimeUnit.MILLISECONDS.sleep(100);
            log.info("消费者1监听到工作队列消息:{}",msg);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 接收工作队列消息2
     */
    @RabbitListener(queues = "work.queue")
    public void workQueueListener2(String msg){
        try {
            TimeUnit.MILLISECONDS.sleep(50);
            log.info("消费者2监听到工作队列消息:{}",msg);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



    /**
     * 接收广播队列消息消费者1
     */
    @RabbitListener(queues = "fanout.queue1")
    public void fanoutQueueListener1(String msg){
        log.info("消费者1监听到广播消息:{}",msg);
    }

    /**
     * 接收广播队列消息消费者2
     */
    @RabbitListener(queues = "fanout.queue1")
    public void fanoutQueueListener2(String msg){
        log.info("消费者2监听到广播消息:{}",msg);
    }

    /**
     * 接收广播队列消息消费者3
     */
    @RabbitListener(queues = "fanout.queue2")
    public void fanoutQueueListener3(String msg){
        log.info("消费者3监听到广播消息:{}",msg);
    }

    /**
     * 接收广播队列消息消费者4
     */
    @RabbitListener(queues = "fanout.queue2")
    public void fanoutQueueListener4(String msg){
        log.info("消费者4监听到广播消息:{}",msg);
    }



    /**
     * 接收路由消息消费者1
     */
    @RabbitListener(queues = "direct.queue1")
    public void directQueueListener1(String msg){
        log.info("消费者1监听到路由消息:{}",msg);
    }

    /**
     * 接收路由消息消费者2
     */
    @RabbitListener(queues = "direct.queue2")
    public void directQueueListener2(String msg){
        log.info("消费者2监听到路由消息:{}",msg);
    }
}
